<?php
class ArcFont
{


    /*
    $fontApi = new ArcFont();
    $fontApi->loadImg('adds.jpg');
    $fontApi->loadText('俞建腾', 12, 'simkai.ttf', 742, 265, 30);
    $fontApi->loadText('俞建腾', 12, 'simkai.ttf', 272, 658, 30);
    $fontApi->outImg('adds_output.jpg');
    echo "<img src='adds_output.jpg' / >";
    */
   


    public $im;
    public function textWidth($txt, $font, $size)
    {

        $bbox = imagettfbbox($size, 0, $font, $txt);

        $w = abs($bbox[4] - $bbox[0]);

        return $w;

    }



    /**
     * @param $im  原图
     * @param $cx  坐标X
     * @param $cy  坐标Y
     * @param $r 圆的半径
     * @param $s 圆的角度范围开始
     * @param $e 圆的角度范围结束
     * @param $txtcol 文字颜色
     * @param $txt 文本内容  
     * @param $font 字体，注意要提供字体文件，可以从fonts目录复制过来调用
     * @param $size 字体大小
     * @param $pad integer 字符间距      
     */
    public function textArcDown($im, $cx, $cy, $r, $s, $e, $txtcol, $txt, $font, $size, $pad = 0)
    {

        $txts = $this->str_split_unicode($txt);

        $tlen = count($txts);

        $arccentre = ($e + $s) / 2;

        $total_width = $this->textWidth($txt, $font, $size) + ($tlen - 1) * $pad;

        $textangle = rad2deg($total_width / $r);

        $s = $arccentre - $textangle / 2;

        $e = $arccentre + $textangle / 2;

        for ($i = 0, $theta = deg2rad($s); $i < $tlen; $i++) {

            $ch = $txts{$i};

            $tx = $cx + $r * cos($theta);

            $ty = $cy + $r * sin($theta);

            $dtheta = ($this->textWidth($ch, $font, $size) + $pad) / $r;

            $angle = rad2deg(M_PI * 3 / 2 - ($dtheta / 2 + $theta));

            imagettftext($im, $size, $angle, $tx, $ty, $txtcol, $font, $ch);

            $theta += $dtheta;

        }

    }



    /**
     * @param $im  原图
     * @param $cx  坐标X
     * @param $cy  坐标Y
     * @param $r 圆的半径
     * @param $s 圆的角度范围开始
     * @param $e 圆的角度范围结束
     * @param $txtcol 文字颜色
     * @param $txt 文本内容  
     * @param $font 字体，注意要提供字体文件，可以从fonts目录复制过来调用
     * @param $size 字体大小
     * @param $pad integer 字符间距      
     */
    
    public function textArcUp($im, $cx, $cy, $r, $s, $e, $txtcol, $txt, $font, $size, $pad = 0)
    {

        $txts = $this->str_split_unicode($txt);

        $tlen = count($txts);

        $arccentre = ($e + $s) / 2;

        $total_width = $this->textWidth($txt, $font, $size) + ($tlen - 1) * $pad;

        $textangle = rad2deg($total_width / $r);

        $s = $arccentre - $textangle / 2;

        $e = $arccentre + $textangle / 2;

        for ($i = 0, $theta = deg2rad($e); $i < $tlen; $i++) {

            $ch = $txts{$i};

            $tx = $cx + $r * cos($theta);

            $ty = $cy + $r * sin($theta);

            $dtheta = ($this->textWidth($ch, $font, $size) + $pad) / $r;

            $angle = rad2deg(M_PI / 2 - ($theta - $dtheta / 2));

            imagettftext($im, $size, $angle, $tx, $ty, $txtcol, $font, $ch);

            $theta -= $dtheta;

        }

    }

    public function str_split_unicode($str, $l = 0)
    {
        if ($l > 0) {
            $ret = array();
            $len = mb_strlen($str, "UTF-8");
            for ($i = 0; $i < $len; $i += $l) {
                $ret[] = mb_substr($str, $i, $l, "UTF-8");
            }
            return $ret;
        }
        return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);
    }

    
 


    /**
    
     * @param $txt 文本内容  
     * @param $fontSize 文字大小  
     * @param $size 字体大小
     * @param $font 字体，注意要提供字体文件，可以从fonts目录复制过来调用
     * @param $x_axi  坐标X
     * @param $y_axi  坐标Y
     * @param $angle  展开角度
     * @param $debug  是否调试
     
     */
    public function loadText($txt, $fontSize = 24, $font = 'simkai.ttf', $x_axi, $y_axi, $angle = 60, $debug = 0)
    {

        $s = $e = 0;

        $white = imagecolorallocate($this->im, 0xFF, 0xFF, 0xFF);

        $grey = imagecolorallocate($this->im, 0xCC, 0xCC, 0xCC);

        $txtcol = imagecolorallocate($this->im, 0xFF, 0x00, 0x00);

        $statTxt = mb_strlen($txt);
        $r       = $statTxt * 10 * 360 / $angle / 2 / pi();

        $x_axi = $x_axi - $r;
        $y_axi = $y_axi - 2 * $r;

        $cx = $r + $x_axi;

        $cy = $r + $y_axi;

        if ($debug) {
            imagearc($this->im, $cx, $cy, $r * 2, $r * 2, $s, $e, $grey);
        }

        $pad = 2; // extra char spacing for text

        $s = (180 - $angle) / 2;

        $e = (180 - $angle) / 2 + $angle;

        $this->textArcUp($this->im, $cx, $cy, $r, $s, $e, $txtcol, $txt, $font, $fontSize, $pad);

    }

    public function loadImg($img)
    {
        $this->im = imagecreatefromjpeg($img);
    }

    public function outImg($outfile)
    {
        imagejpeg($this->im, $outfile);
        imagedestroy($this->im);
    }



}

